package com.heima.article.stream.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.config.stream.KafkaStreamListener;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.model.mess.ArticleVisitStreamMess;
import com.heima.model.mess.app.UpdateArticleMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.kstream.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Slf4j
public class HotArticleStreamHandler implements KafkaStreamListener<KStream<String,String>> {

    //流的源头
    @Override
    public String listenerTopic() {
        return HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC;
    }

    //流的输出  {"articleId":1232323232323,"like":1001}
    @Override
    public String sendTopic() {
        return HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC;
    }

    //流处理的业务逻辑
    @Override
    public KStream<String, String> getService(KStream<String, String> stream) {
        return stream.flatMapValues(new ValueMapper<String, Iterable<String>>() {
            @Override
            public Iterable<String> apply(String value) {
                //UpdateArticleMess
                UpdateArticleMess updateArticleMess = JSON.parseObject(value, UpdateArticleMess.class);
                log.info("recevie message value:{}",updateArticleMess);
                //按照文章的id和行为进行聚合查询  --》  1232323232323:LIEKS
                return Arrays.asList(updateArticleMess.getArticleId()+":"+updateArticleMess.getType());
            }
        }).map(new KeyValueMapper<String, String, KeyValue<String, String>>() {
            @Override
            public KeyValue<String, String> apply(String key, String value) {
                //  key 1232323232323:LIEKS
                //  value 1232323232323:LIEKS
                return new KeyValue<>(value,value);
            }
        }).groupByKey().windowedBy(TimeWindows.of(10000))
                .count(Materialized.as("count-article-num-00008"))
                .toStream().map((key,value)->{
                    // key   1232323232323:LIEKS
                    // value  {"articleId":1232323232323,"like":1001}
                   return new KeyValue<>(key.key().toString(),formatObj(key.key().toString(),value.toString()));
        });
    }

    /**
     * 封装消息
     * @param key  1232323232323:LIEKS
     * @param value  5
     * @return
     */
    private String formatObj(String key, String value) {
        ArticleVisitStreamMess mess = new ArticleVisitStreamMess();
        String[] split = key.split(":");
        //文章id
        mess.setArticleId(Long.valueOf(split[0]));
        if(split[1].equals(UpdateArticleMess.UpdateArticleType.LIKES.name())){
            mess.setLike(Long.valueOf(value));
        }
        if(split[1].equals(UpdateArticleMess.UpdateArticleType.VIEWS.name())){
            mess.setView(Long.valueOf(value));
        }
        if(split[1].equals(UpdateArticleMess.UpdateArticleType.COLLECTION.name())){
            mess.setCollect(Long.valueOf(value));
        }
        if(split[1].equals(UpdateArticleMess.UpdateArticleType.COMMENT.name())){
            mess.setComment(Long.valueOf(value));
        }

        log.info("处理之后的结果为:{}",mess);

        return JSON.toJSONString(mess);
    }
}